home *** CD-ROM | disk | FTP | other *** search
Wrap
package mx.rpc.http { import flash.utils.getQualifiedClassName; import flash.xml.XMLDocument; import flash.xml.XMLNode; import mx.collections.ArrayCollection; import mx.core.mx_internal; import mx.messaging.ChannelSet; import mx.messaging.channels.DirectHTTPChannel; import mx.messaging.config.LoaderConfig; import mx.messaging.messages.HTTPRequestMessage; import mx.messaging.messages.IMessage; import mx.rpc.AbstractInvoker; import mx.rpc.AsyncDispatcher; import mx.rpc.AsyncRequest; import mx.rpc.AsyncToken; import mx.rpc.Fault; import mx.rpc.events.FaultEvent; import mx.rpc.xml.SimpleXMLDecoder; import mx.rpc.xml.SimpleXMLEncoder; import mx.utils.ObjectProxy; import mx.utils.StringUtil; import mx.utils.Translator; import mx.utils.URLUtil; use namespace mx_internal; public class HTTPService extends AbstractInvoker { private static var _directChannelSet:ChannelSet; public static const RESULT_FORMAT_E4X:String = "e4x"; public static const RESULT_FORMAT_FLASHVARS:String = "flashvars"; public static const RESULT_FORMAT_OBJECT:String = "object"; public static const RESULT_FORMAT_ARRAY:String = "array"; public static const RESULT_FORMAT_TEXT:String = "text"; public static const RESULT_FORMAT_XML:String = "xml"; public static const CONTENT_TYPE_XML:String = "application/xml"; public static const CONTENT_TYPE_FORM:String = "application/x-www-form-urlencoded"; public static const DEFAULT_DESTINATION_HTTP:String = "DefaultHTTP"; public static const DEFAULT_DESTINATION_HTTPS:String = "DefaultHTTPS"; public static const ERROR_URL_REQUIRED:String = "Client.URLRequired"; public static const ERROR_DECODING:String = "Client.CouldNotDecode"; public static const ERROR_ENCODING:String = "Client.CouldNotEncode"; private static const TRANSLATOR:Translator = Translator.getDefaultInstanceFor(HTTPService); public var xmlDecode:Function; public var xmlEncode:Function; public var headers:Object; public var request:Object; private var _useProxy:Boolean = false; private var _url:String; public var contentType:String = "application/x-www-form-urlencoded"; public var method:String = "GET"; private var _resultFormat:String = "object"; mx_internal var _rootURL:String; public function HTTPService(param1:String = null) { contentType = CONTENT_TYPE_FORM; headers = {}; method = HTTPRequestMessage.GET_METHOD; request = {}; _resultFormat = RESULT_FORMAT_OBJECT; _useProxy = false; super(); mx_internal::asyncRequest = new AsyncRequest(); makeObjectsBindable = true; if(URLUtil.isHttpsURL(LoaderConfig.url)) { mx_internal::asyncRequest.destination = DEFAULT_DESTINATION_HTTPS; } else { mx_internal::asyncRequest.destination = DEFAULT_DESTINATION_HTTP; } } public function get resultFormat() : String { return _resultFormat; } public function send(param1:Object = null) : AsyncToken { var _loc2_:Object = null; var _loc3_:AsyncToken = null; var _loc4_:Fault = null; var _loc5_:FaultEvent = null; var _loc6_:HTTPRequestMessage = null; var _loc7_:Object = null; var _loc8_:SimpleXMLEncoder = null; var _loc9_:XMLDocument = null; var _loc10_:Array = null; var _loc11_:int = 0; var _loc12_:Object = null; var _loc13_:String = null; var _loc14_:ChannelSet = null; if(param1 == null) { param1 = request; } if(contentType == CONTENT_TYPE_XML) { if(!(param1 is XMLNode) && !(param1 is XML)) { if(xmlEncode != null) { _loc7_ = xmlEncode(param1); if(null == _loc7_) { _loc3_ = new AsyncToken(null); _loc4_ = new Fault(ERROR_ENCODING,TRANSLATOR.textOf("xmlEncodeReturnNull")); _loc5_ = FaultEvent.createEvent(_loc4_,_loc3_); new AsyncDispatcher(mx_internal::dispatchRpcEvent,[_loc5_],10); return _loc3_; } if(!(_loc7_ is XMLNode)) { _loc3_ = new AsyncToken(null); _loc4_ = new Fault(ERROR_ENCODING,TRANSLATOR.textOf("xmlEncodeReturnNoXMLNode")); _loc5_ = FaultEvent.createEvent(_loc4_,_loc3_); new AsyncDispatcher(mx_internal::dispatchRpcEvent,[_loc5_],10); return _loc3_; } _loc2_ = XMLNode(_loc7_).toString(); } else { _loc8_ = new SimpleXMLEncoder(null); _loc9_ = new XMLDocument(); _loc10_ = _loc8_.encodeValue(param1,new QName(null,"encoded"),new XMLNode(1,"top")).childNodes.concat(); _loc11_ = 0; while(_loc11_ < _loc10_.length) { _loc9_.appendChild(_loc10_[_loc11_]); _loc11_++; } _loc2_ = _loc9_.toString(); } } else { _loc2_ = param1.toString(); } } else if(contentType == CONTENT_TYPE_FORM) { _loc2_ = {}; for(_loc13_ in param1) { _loc12_ = param1[_loc13_]; if(_loc12_ != null) { _loc2_[_loc13_] = _loc12_ is Object ? _loc12_.toString() : _loc12_; } } } else { _loc2_ = param1; } _loc6_ = new HTTPRequestMessage(); if(useProxy) { if(Boolean(url) && url != "") { _loc6_.url = URLUtil.getFullURL(rootURL,url); } } else { if(!url) { _loc3_ = new AsyncToken(null); _loc4_ = new Fault(ERROR_URL_REQUIRED,TRANSLATOR.textOf("urlNotSpecified")); _loc5_ = FaultEvent.createEvent(_loc4_,_loc3_); new AsyncDispatcher(mx_internal::dispatchRpcEvent,[_loc5_],10); return _loc3_; } if(!useProxy) { _loc14_ = getDirectChannelSet(); if(_loc14_ != mx_internal::asyncRequest.channelSet) { mx_internal::asyncRequest.channelSet = _loc14_; } } _loc6_.url = url; } _loc6_.contentType = contentType; _loc6_.method = method.toUpperCase(); if(contentType == CONTENT_TYPE_XML && _loc6_.method == HTTPRequestMessage.GET_METHOD) { _loc6_.method = HTTPRequestMessage.POST_METHOD; } _loc6_.body = _loc2_; _loc6_.httpHeaders = headers; return mx_internal::invoke(_loc6_); } public function logout() : void { mx_internal::asyncRequest.logout(); } private function decodeArray(param1:Object) : Object { var _loc2_:Array = null; if(param1 is Array) { _loc2_ = param1 as Array; } else { if(param1 is ArrayCollection) { return param1; } _loc2_ = []; _loc2_.push(param1); } if(makeObjectsBindable) { return new ArrayCollection(_loc2_); } return _loc2_; } public function get destination() : String { return mx_internal::asyncRequest.destination; } public function setRemoteCredentials(param1:String, param2:String) : void { mx_internal::asyncRequest.setRemoteCredentials(param1,param2); } public function set destination(param1:String) : void { useProxy = true; mx_internal::asyncRequest.destination = param1; } public function disconnect() : void { mx_internal::asyncRequest.disconnect(); } public function set requestTimeout(param1:int) : void { if(mx_internal::asyncRequest.requestTimeout != param1) { mx_internal::asyncRequest.requestTimeout = param1; } } private function getDirectChannelSet() : ChannelSet { var _loc1_:ChannelSet = null; if(_directChannelSet == null) { _loc1_ = new ChannelSet(); _loc1_.addChannel(new DirectHTTPChannel("direct_http_channel")); _directChannelSet = _loc1_; } return _directChannelSet; } public function get useProxy() : Boolean { return _useProxy; } public function get rootURL() : String { if(mx_internal::_rootURL == null) { mx_internal::_rootURL = LoaderConfig.url; } return mx_internal::_rootURL; } public function setCredentials(param1:String, param2:String) : void { mx_internal::asyncRequest.setCredentials(param1,param2); } public function set channelSet(param1:ChannelSet) : void { useProxy = true; mx_internal::asyncRequest.channelSet = param1; } private function decodeParameterString(param1:String) : Object { var _loc2_:String = null; var _loc3_:Array = null; var _loc4_:Object = null; var _loc5_:int = 0; var _loc6_:String = null; var _loc7_:int = 0; var _loc8_:String = null; var _loc9_:String = null; _loc2_ = StringUtil.trim(param1); _loc3_ = _loc2_.split("&"); _loc4_ = {}; _loc5_ = 0; while(_loc5_ < _loc3_.length) { _loc6_ = _loc3_[_loc5_]; _loc7_ = int(_loc6_.indexOf("=")); if(_loc7_ != -1) { _loc8_ = unescape(_loc6_.substr(0,_loc7_)); _loc8_ = _loc8_.split("+").join(" "); _loc9_ = unescape(_loc6_.substr(_loc7_ + 1)); _loc9_ = _loc9_.split("+").join(" "); _loc4_[_loc8_] = _loc9_; } _loc5_++; } return _loc4_; } public function set useProxy(param1:Boolean) : void { var _loc2_:ChannelSet = null; if(param1 != _useProxy) { _useProxy = param1; _loc2_ = getDirectChannelSet(); if(!useProxy) { if(_loc2_ != mx_internal::asyncRequest.channelSet) { mx_internal::asyncRequest.channelSet = _loc2_; } } else if(mx_internal::asyncRequest.channelSet == _loc2_) { mx_internal::asyncRequest.channelSet = null; } } } public function get requestTimeout() : int { return mx_internal::asyncRequest.requestTimeout; } public function get channelSet() : ChannelSet { return mx_internal::asyncRequest.channelSet; } public function set url(param1:String) : void { _url = param1; } public function set resultFormat(param1:String) : void { switch(param1) { case RESULT_FORMAT_OBJECT: case RESULT_FORMAT_ARRAY: case RESULT_FORMAT_XML: case RESULT_FORMAT_E4X: case RESULT_FORMAT_TEXT: case RESULT_FORMAT_FLASHVARS: _resultFormat = param1; return; default: throw new ArgumentError(TRANSLATOR.textOf("invalidResultFormat",param1,RESULT_FORMAT_OBJECT,RESULT_FORMAT_ARRAY,RESULT_FORMAT_XML,RESULT_FORMAT_E4X,RESULT_FORMAT_TEXT,RESULT_FORMAT_FLASHVARS)); } } public function set rootURL(param1:String) : void { mx_internal::_rootURL = param1; } override mx_internal function processResult(param1:IMessage, param2:AsyncToken) : Boolean { var body:Object = null; var tmp:Object = null; var fault:Fault = null; var decoded:Object = null; var fault1:Fault = null; var decoder:SimpleXMLDecoder = null; var fault2:Fault = null; var fault3:Fault = null; var message:IMessage = param1; var token:AsyncToken = param2; body = message.body; if(body == null || body != null && body is String && StringUtil.trim(String(body)) == "") { mx_internal::_result = body; return true; } if(body is String) { if(resultFormat == RESULT_FORMAT_XML || resultFormat == RESULT_FORMAT_OBJECT || resultFormat == RESULT_FORMAT_ARRAY) { tmp = new XMLDocument(); XMLDocument(tmp).ignoreWhite = true; try { XMLDocument(tmp).parseXML(String(body)); } catch(parseError:Error) { fault = new Fault(ERROR_DECODING,parseError.message); mx_internal::dispatchRpcEvent(FaultEvent.createEvent(fault,token,message)); return false; } if(resultFormat == RESULT_FORMAT_OBJECT || resultFormat == RESULT_FORMAT_ARRAY) { if(xmlDecode != null) { decoded = xmlDecode(tmp); if(decoded == null) { fault1 = new Fault(ERROR_DECODING,TRANSLATOR.textOf("xmlDecodeReturnNull")); mx_internal::dispatchRpcEvent(FaultEvent.createEvent(fault1,token,message)); } } else { decoder = new SimpleXMLDecoder(makeObjectsBindable); decoded = decoder.decodeXML(XMLNode(tmp)); if(decoded == null) { fault2 = new Fault(ERROR_DECODING,TRANSLATOR.textOf("defaultDecoderFailed")); mx_internal::dispatchRpcEvent(FaultEvent.createEvent(fault2,token,message)); } } if(decoded == null) { return false; } if(makeObjectsBindable && getQualifiedClassName(decoded) == "Object") { decoded = new ObjectProxy(decoded); } else { ; } if(resultFormat == RESULT_FORMAT_ARRAY) { decoded = decodeArray(decoded); } mx_internal::_result = decoded; } else { if(tmp.childNodes.length == 1) { tmp = tmp.firstChild; } mx_internal::_result = tmp; } } else if(resultFormat == RESULT_FORMAT_E4X) { try { mx_internal::_result = new XML(String(body)); } catch(error:Error) { fault3 = new Fault(ERROR_DECODING,error.message); mx_internal::dispatchRpcEvent(FaultEvent.createEvent(fault3,token,message)); return false; } } else if(resultFormat == RESULT_FORMAT_FLASHVARS) { mx_internal::_result = decodeParameterString(String(body)); } else { mx_internal::_result = body; } } else { if(resultFormat == RESULT_FORMAT_ARRAY) { body = decodeArray(body); } mx_internal::_result = body; } return true; } public function get url() : String { return _url; } } }